package com.itheima.类加载与反射.反射.案例五;

import java.lang.reflect.Constructor;
import java.lang.reflect.Field;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;

public class 反射方法 {
    public static void main(String[] args) throws ClassNotFoundException, NoSuchMethodException, IllegalAccessException, InvocationTargetException, InstantiationException {
        Class cl = Class.forName("com.itheima.类加载与反射.反射.案例五.Student");
        //method1(cl);
        //method2(cl);
        //method3(cl);
        Method show = cl.getDeclaredMethod("show", String.class);
        show.setAccessible(true);
        Constructor con = cl.getDeclaredConstructor();
        Student stu = (Student) con.newInstance();
        String nddwm = (String) show.invoke(stu, "刘观圣");
        System.out.println(nddwm);

    }

    public static void method3(Class cl) throws NoSuchMethodException, InstantiationException, IllegalAccessException, InvocationTargetException {
        Method toString = cl.getMethod("toString");
//        System.out.println(toString);
        Constructor con = cl.getConstructor();
        Student stu = (Student) con.newInstance();
        Object invoke = toString.invoke(stu);
        System.out.println(invoke);
    }

    public static void method2(Class cl) {
        Method[] methods = cl.getMethods();
        for (Method method : methods) {
            System.out.println(method);
        }
    }

    public static void method1(Class cl) {
        Method[] ds = cl.getDeclaredMethods();
        for (Method d : ds) {
            System.out.println(d);
        }
    }
}
